
from pydantic import BaseModel,Field,field_validator
from datetime import date
from typing import List,Optional

class UserModel(BaseModel):
    name: str
    age: int = Field(default=1,gt=1,lt=110)
    birth: Optional[date] = None
    friends: List[int]

    # 校验
    @field_validator('name')
    def name_must_alpha(cls,value):
        assert value.isalpha(),'姓名必须是字母'
        return value
